package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.PermissionDao;
import com.atguigu.dao.RolePermissionDao;
import com.atguigu.entity.Permission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hljstart
 * @create 2022-06-13-14:19
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionsByRoleId(Long roleId) {
        //获取所有的权限
        List<Permission> permissionList=permissionDao.findAll();
        //根据角色id查询已分配的权限的id
        List<Long> permissionIds=rolePermissionDao.findPermissionIdsByRoleId(roleId);
        //创建返回的list
        List<Map<String,Object>> returnList=new ArrayList<>();
        //遍历所有的权限
        for (Permission permission : permissionList) {
            //     { id:22, pId:2, name:"随意勾选 2-2", open:true},
            //创建一个map
            Map<String, Object> map = new HashMap<>();
            map.put("id",permission.getId());
            map.put("pId",permission.getParentId());
            map.put("name",permission.getName());
            //判断当前权限的id在不在permissionIds中
            if(permissionIds.contains(permission.getId())){
                //证明该权限是已经分配的权限
                map.put("checked",true);
            }
            //将map放入到返回的list中
            returnList.add(map);
        }
        return returnList;
    }

    @Override
    public void assignPermission(Long roleId, Long[] permissionIds) {
        //调用RolePermissionDao中根据角色id删除已分配权限的方法
        rolePermissionDao.deletePermissionIdsByRoleId(roleId);
        //遍历所有的权限id
        for (Long permissionId : permissionIds) {
            //调用RolePermissionDao中保存权限id和角色id的方法
            rolePermissionDao.addRoleIdAndPermission(roleId,permissionId);

        }
    }

    @Override
    public List<Permission> getMenuPermissionsByAdminId(Long userId) {

        List<Permission> permissionList=null;
        //判断是否是系统管理员
        if(userId==1){
            //证明是系统管理员,获取所有的权限菜单
            permissionList = permissionDao.findAll();
        }else {
            //根据用户的id查询权限菜单
            permissionList=permissionDao.getMenuPermissionsByAdminId(userId);
        }
        //通过PermissionHelper工具类将List转成树形结构
        List<Permission> treeList = PermissionHelper.bulid(permissionList);
        return treeList;
    }

    @Override
    public List<String> getPermissionCodesByAdminId(Long id) {
        List<String> permissionCodes=null;
        if(id==1){
            permissionCodes=permissionDao.getAllPermissionCodes();
        }else {
            //根据用户id查询权限
            permissionCodes=permissionDao.getPermissionCodeByAdminId(id);
        }
        return permissionCodes;
    }
}
